package cn.kgc.boot.util;

import lombok.extern.slf4j.Slf4j;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author XMF
 * @date 2021-08-09 11:25
 **/
@Slf4j
public class CacheUtilDemo {


    /**
     *
     * maxSize:map的最大长度,添加第1001个entry时,会导致第1个马上过期(即使没到过期时间)
     * expiration:过期时间和过期单位,设置过期时间,则永久有效.
     * expirationPolicy:过期策略的使用
     *      CREATED：  在每次更新元素时，过期倒计时清零
     *      ACCESSED： 在每次访问元素时，过期倒计时清零
     *
     * variableExpiration:允许更新过期时间值,如果不设置variableExpiration
     *      不允许更改过期时间,一旦执行更改过期时间的操作则会抛出UnsupportedOperationException异常
     * expirationListener:同步过期提醒
     * asyncExpirationListener:异步过期提醒
     * entryLoader:懒加载,调用get方法时若key不存在创建默认value
     *
     * ExpiringMap<String,String> map = ExpiringMap.builder()
     *             .maxSize(1000)
     *             .expiration(1, TimeUnit.SECONDS)
     *             .expirationPolicy(ExpirationPolicy.ACCESSED)
     *             .variableExpiration()
     *             .expirationListener(ExpiringMapTest::remindExpiration)
     *             .asyncExpirationListener(ExpiringMapTest::remindAsyncExpiration)
     *             .entryLoader(name -> "default")
     *             .build();
     *
     */

    public static Queue queue = new ArrayBlockingQueue(16);

    public static ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();


    private ExpiringMap<String, Long> cacheMapX = ExpiringMap.builder()
            .maxSize(1000)
            .expiration(1, TimeUnit.SECONDS)
            .expirationPolicy(ExpirationPolicy.CREATED)
            .variableExpiration()
            .build();

    private CacheUtilDemo(){}


    private static class GetCacheUtilDemo{
        private static final CacheUtilDemo CACHE_UTIL_DEMO = new CacheUtilDemo();
    }

    public static CacheUtilDemo getInstance(){
        return GetCacheUtilDemo.CACHE_UTIL_DEMO;
    }

    public ExpiringMap<String, Long> getCacheMap(){
        return CacheUtilDemo.getInstance().cacheMapX;
    }

    public void setCacheMap(String key, Long val){
        CacheUtilDemo.getInstance().cacheMapX.put(key, val);
    }













}
